package com.zyc.webtest.controller;

import com.zyc.webtest.domain.SysUserEntity;
import com.zyc.webtest.service.SysUserService;
import com.zyc.webtest.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 系统用户
 * 
 * @author zyc
 * @date 2017-04-29 13:32:48
 */
@Controller
@RequestMapping("sysuser")
public class SysUserController extends BaseController{

	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> params){

		return "sysuser/list";
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{userId}")
	public String info(@PathVariable("userId") Long userId){
		SysUserEntity sysUserEntity = sysUserService.findById(userId).orElse(new SysUserEntity());
        return "sysuser/info/"+userId;
	}
	
	/**
	 * 保存
	 */
	@PostMapping("/save")
    @ResponseBody
	public ResultMsg save(@RequestBody SysUserEntity sysUser){
        return sysUserService.save(sysUser).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/update")
    @ResponseBody
	public ResultMsg update(@RequestBody SysUserEntity sysUser){
        return sysUserService.save(sysUser).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/delete")
    @ResponseBody
	public ResultMsg delete(@PathVariable("userId") Long[] userId){
        ResultMsg rm = null;
        try {
			sysUserService.delete(userId);
            rm = ResultMsg.success();
        } catch (Exception e) {
            logger.error("",e);
            rm = ResultMsg.error();
        }
        return rm;
	}
	
}
